package com.zh.excel.util;

import com.csvreader.CsvWriter;
import com.zh.excel.entity.QrCodeTransEntity;
import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.T;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 导出Excel文件工具
 * @Author: Li.HuiNeng
 * @Create in: 2020-02-19 16:09
 * @Version: 1.0.1
 */
public class CsvFileUtil {
/*
    public static int exportCsv(String csvName, String headArr, String[] headerArr,List<QrCodeTransEntity> list) {
        String filePath = "E:\\TestCsvDirectory"; //CSV文件路径
        String fileName = csvName + ".csv";//CSV文件名称
        File csvFile = null;
        BufferedWriter csvWriter = null;
        try {
            csvFile = new File(filePath + File.separator + fileName);
            File parent = csvFile.getParentFile();
            if (parent != null && !parent.exists()) {
                parent.mkdirs();
            }
            csvFile.createNewFile();

            // GB2312使正确读取分隔符","
            csvWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), "GB2312"), 1024);

            // 写入文件头部标题行
            csvWriter.write(String.join(",", headArr));
            csvWriter.newLine();
            // 写入表头
            csvWriter.write(headerArr);
            // 写入文件内容
            for (QrCodeTransEntity points : list) {
                csvWriter.write(points.toString());
                csvWriter.newLine();
            }
            csvWriter.flush();
            return 1;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        } finally {
            try {
                csvWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }*/

    /**
     * 创建临时的csv文件
     *
     * @return
     * @throws IOException
     */
    public static File createQrCodeTransTempFile(List<QrCodeTransEntity> list, String csvName, String[] headers) throws IOException {
        File tempFile = File.createTempFile(csvName, ".csv");
        CsvWriter csvWriter = new CsvWriter(tempFile.getCanonicalPath(), ',', StandardCharsets.UTF_8);
        // 写表头
        csvWriter.writeRecord(headers);
        for (QrCodeTransEntity data : list) {
            csvWriter.write(data.getId().toString());
            csvWriter.write(data.getTransDate());
            csvWriter.write(data.getBranchName());
            csvWriter.write(data.getAgentName());
            csvWriter.write(data.getMerchName());
            csvWriter.write(interTypeConver(data.getInterType()));
            csvWriter.write(scanTypeConver(data.getScanType()));
            csvWriter.write(payTypeConver(data.getPayType()));
            csvWriter.write(settleTypeConver(data.getSettleType()));
            csvWriter.write(data.getAmount().toString());
            csvWriter.write(data.getNum().toString());
            csvWriter.write(data.getFee().toString());
            csvWriter.endRecord();
        }
        csvWriter.close();
        return tempFile;
    }

    /**
     * 写入csv结束，写出流
     */
    public static void outCsvStream(HttpServletResponse response, File tempFile) throws IOException {
        java.io.OutputStream out = response.getOutputStream();
        byte[] b = new byte[10240];
        java.io.File fileLoad = new java.io.File(tempFile.getCanonicalPath());
        response.reset();
        response.setContentType("application/csv");
        response.setHeader("content-disposition", "attachment; filename=export.csv");
        java.io.FileInputStream in = new java.io.FileInputStream(fileLoad);
        int n;
        //为了保证excel打开csv不出现中文乱码
        out.write(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
        while ((n = in.read(b)) != -1) {
            //每次写入out1024字节
            out.write(b, 0, n);
        }
        in.close();
        out.close();
    }

    /**
     * 删除单个文件
     *
     * @return 单个文件删除成功返回true，否则返回false
     */
    public static boolean deleteFile(File file) {
        // 如果文件路径所对应的文件存在，并且是一个文件，则直接删除
        if (file.exists() && file.isFile()) {
            return file.delete();
        } else {
            return false;
        }
    }

    /**
     * 接口类型转换
     *
     * @param interType
     * @return
     */
    private static String interTypeConver(Integer interType) {
        String str = "-";
        switch (interType) {
            case 1:
                str = "手机app";
                break;
            case 2:
                str = "接口";
                break;
            case 4:
                str = "平台";
                break;
            case 8:
                str = "一付码";
                break;
        }
        return str;
    }

    /**
     * 扫码类型转换
     *
     * @param scanType
     * @return
     */
    private static String scanTypeConver(Integer scanType) {
        String str = "-";
        switch (scanType) {
            case 1:
                str = "主扫";
                break;
            case 2:
                str = "被扫";
                break;
            case 4:
                str = "公众号";
                break;
            case 8:
                str = "WAP";
                break;
            case 16:
                str = "APP";
                break;
        }
        return str;
    }

    /**
     * 支付类型转换
     *
     * @param payType
     * @return
     */
    private static String payTypeConver(Integer payType) {
        String str = "-";
        switch (payType) {
            case 1:
                str = "支付宝";
                break;
            case 2:
                str = "微信";
                break;
            case 4:
                str = "百度";
                break;
            case 8:
                str = "QQ";
                break;
            case 16:
                str = "京东";
                break;
            case 32:
                str = "银联";
                break;
        }
        return str;
    }

    /**
     * 结算类型转换
     * @param settleType
     * @return
     */
    private static String settleTypeConver(Integer settleType) {
        String str = "-";
        switch (settleType) {
            case 1:
                str = "T+0";
                break;
            case 2:
                str = "T+1";
                break;
            case 4:
                str = "T+5";
                break;
        }
        return str;
    }
}
